﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using mogco.Visuals;
using mogco.Structs;

namespace mogco.Structs
{
    public partial class SettingsWindow : Form
    {
        Hashtable ht = new Hashtable();
        Hashtable sf = new Hashtable();
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        CMPMainWorker worker;
       
        public SettingsWindow(CMPMainWorker _worker)
        {
            worker = _worker;
            InitializeComponent();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Settings.xml.JPGQuality = trackBar1.Value;
            label4.Text = trackBar1.Value.ToString();
        }

        private void SettingsWindow_Load(object sender, EventArgs e)
        {
            textBox2.Text = Settings.xml.SavePath;
            dialog.SelectedPath = Settings.xml.SavePath;
            trackBar1.Value = Settings.xml.JPGQuality;
            label4.Text = Settings.xml.JPGQuality.ToString();
            textBox1.Text = Settings.xml.FilesMask;
            checkBox1.Checked = Settings.xml.SearchFilesRecursively;
            checkBox2.Checked = Settings.xml.RewriteExistsFiles;
            CDSC.Checked = Settings.xml.UseDirectoryTree;
            if ((Settings.xml.OpenPictureExtensions & Structs.PictureExtensions.JPG) == Structs.PictureExtensions.JPG)
                CBJPG.Checked = true;
            if ((Settings.xml.OpenPictureExtensions & Structs.PictureExtensions.PNG) == Structs.PictureExtensions.PNG)
                CBPNG.Checked = true;
            sf.Add("JPG", Structs.PictureExtensions.JPG);
            sf.Add("PNG", Structs.PictureExtensions.PNG);
            
            foreach (var key in sf.Keys)
            {
                comboConvertTo.Items.Add(key);
                if (sf[key].Equals(Settings.xml.SaveFormat))
                    comboConvertTo.SelectedText = (string)key;
            }

            foreach (Point resolution in Settings.xml.ResolutionArray)
            {
                string key = resolution.Y.ToString() + "x" + resolution.X.ToString();
                if (!ht.ContainsKey(key))
                    ht.Add(key, resolution);
                else
                    Logger.txtLogger.WriteMessage("Warning: hashtable contains such resolution");
            }

            foreach (var key in ht.Keys)
            {
                comboBox1.Items.Add(key.ToString());
            }
            // default value
            string default_key_string = Settings.xml.Resolution.Y.ToString() + "x" + Settings.xml.Resolution.X.ToString();
            comboBox1.SelectedText = default_key_string;

            foreach (Interface.IPlugin plugin in worker.Plugins.Values)
            {
                PluginsGrid.Rows.Add(true, plugin.Name, plugin.Author);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Settings.xml.FilesMask = textBox1.Text;
            Settings.Save();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Settings.Reload();
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Settings.xml.FilesMask = textBox1.Text;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            Settings.xml.Resolution = (Point)ht[comboBox1.SelectedItem];
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            DialogResult res = dialog.ShowDialog();
            if (res == DialogResult.OK)
            {
                textBox2.Text = dialog.SelectedPath;
                Settings.xml.SavePath = dialog.SelectedPath;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Settings.xml.SearchFilesRecursively = checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            Settings.xml.RewriteExistsFiles = checkBox2.Checked;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            AddNewResolution anrWindow = new AddNewResolution();
            anrWindow.ShowDialog();
            Settings.xml.ResolutionArray.Add(anrWindow.GetNewResolution);
            if (anrWindow.DialogResult == DialogResult.OK)
            {
                string new_key = anrWindow.GetNewResolution.Y.ToString() + "x" + anrWindow.GetNewResolution.X.ToString();
                ht.Add(new_key, anrWindow.GetNewResolution);
                Settings.xml.ResolutionArray.Add(anrWindow.GetNewResolution);
                comboBox1.Items.Add(new_key);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                Settings.xml.ResolutionArray.Remove((Point)ht[comboBox1.SelectedItem]);
                ht.Remove(comboBox1.SelectedItem);
                comboBox1.Items.Remove(comboBox1.SelectedItem);
                if (comboBox1.Items.Count > 0)
                    comboBox1.SelectedIndex = 0;
            }
        }

        private void CBJPG_CheckedChanged(object sender, EventArgs e)
        {
            if (CBJPG.Checked)
                Settings.xml.OpenPictureExtensions = Settings.xml.OpenPictureExtensions | mogco.Structs.Structs.PictureExtensions.JPG;
            else
                Settings.xml.OpenPictureExtensions = Settings.xml.OpenPictureExtensions ^ mogco.Structs.Structs.PictureExtensions.JPG;
        }

        private void CBPNG_CheckedChanged(object sender, EventArgs e)
        {
            if (CBPNG.Checked)
                Settings.xml.OpenPictureExtensions = Settings.xml.OpenPictureExtensions | mogco.Structs.Structs.PictureExtensions.PNG;
            else
                Settings.xml.OpenPictureExtensions = Settings.xml.OpenPictureExtensions ^ mogco.Structs.Structs.PictureExtensions.PNG;
        }

        private void comboConvertTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Settings.xml.SaveFormat = (Structs.PictureExtensions)sf[comboConvertTo.SelectedItem];
        }

        private void CDSC_CheckedChanged(object sender, EventArgs e)
        {
            Settings.xml.UseDirectoryTree = CDSC.Checked;
        }
    }
}
